/*
 * 坦克大战
 * 1.画出坦克
 * 2.我的坦克上下左右移动
 * 3.坦克发射子弹
 * 4.子弹击中敌人的坦克，坦克爆炸消失，
 * 5.防止敌人坦克重叠运动
 * 6.分关
 * 7.开始panel，会闪烁
 * 8.游戏暂停和继续
 * 9.记录玩家成绩
 * 10.java操作声音文件
 */

package tankgame.tankgame3;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MyTankGame3 extends JFrame implements ActionListener {
	MyPanel mp;
	MyStartPanel msp;
	JMenuBar jmb;
	JMenu jm1;
	JMenuItem jmi1, jmi2, jmi3, jmi4;

	public static void main(String[] args) {
		MyTankGame3 myTankGame3 = new MyTankGame3();
	}

	public MyTankGame3() {
		jmb = new JMenuBar();
		jm1 = new JMenu("游戏(G)");
		jm1.setMnemonic('G');
		jmi1 = new JMenuItem("开始新游戏(N)");
		jmi1.setMnemonic('N');
		jmi1.addActionListener(this);
		jmi1.setActionCommand("start new game");
		jmi2 = new JMenuItem("退出游戏(E)");
		jmi2.setMnemonic('E');
		jmi2.addActionListener(this);
		jmi2.setActionCommand("exit game");
		jmi3 = new JMenuItem("存盘退出(S)");
		jmi3.setMnemonic('S');
		jmi3.addActionListener(this);
		jmi3.setActionCommand("save game");
		jmi4 = new JMenuItem("恢复游戏(C)");
		jmi4.setMnemonic('C');
		jmi4.addActionListener(this);
		jmi4.setActionCommand("continue game");
		jm1.add(jmi1);
		jm1.add(jmi2);
		jm1.add(jmi3);
		jm1.add(jmi4);
		jmb.add(jm1);
		this.setJMenuBar(jmb);
		msp = new MyStartPanel();
		this.add(msp);
		Thread t = new Thread(msp);
		t.start();

		this.setSize(600, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("start new game") && mp == null) {
			new AudioPlay("D:\\python\\workspace\\pycharm\\12声音\\GameBGMs\\坦克大战.wav").start();
			Record.readRecord();
			mp = new MyPanel();
			Thread t = new Thread(mp);
			t.start();
			this.remove(msp);
			this.add(mp);
			this.addKeyListener(mp);
			this.setVisible(true);
		} else if (e.getActionCommand().equals("exit game")) {
			Record.saveRecord();
			System.exit(0);
		} else if (e.getActionCommand().equals("save game")) {
			Record.enemies = this.mp.enemies;
			Record.saveGame();
			System.exit(0);
		} else if (e.getActionCommand().equals("continue game") && mp == null) {
			Record.continueGame();
			mp = new MyPanel(Record.enemies, Record.enemies.size());
			Thread t = new Thread(mp);
			t.start();
			this.remove(msp);
			this.add(mp);
			this.addKeyListener(mp);
			this.setVisible(true);
		}
	}
}

class MyStartPanel extends JPanel implements Runnable {
	int time = 0;

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		g.fillRect(0, 0, 400, 300);
		if (time % 2 == 0) {
			g.setColor(Color.YELLOW);
			Font myFont = new Font("华文新魏", Font.BOLD, 30);
			g.setFont(myFont);
			g.drawString("stage:1", 150, 150);
		}
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(200);
				time++;
				if (time >= 100) {
					time = 0;
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.repaint();
		}

	}
}

class MyPanel extends JPanel implements KeyListener, Runnable {
	Hero hero;// 我方坦克
	Vector<Enemy> enemies = new Vector<Enemy>();// 敌方坦克
	int enemSize = 10;// 敌方坦克数量
	Vector<Bomb> bombs = new Vector<Bomb>();// 爆炸数量
	Image img1, img2, img3;// 爆炸图片

	public MyPanel() {

		hero = new Hero(100, 270, 0, 3, Color.YELLOW);// 创建我方坦克
		for (int i = 0; i < enemSize; i++) {// 创建敌方坦克
			Enemy e = new Enemy((i + 1) * 40, 0, 1, 1, Color.BLUE);
			e.setEnemies(enemies);
			new Thread(e).start();
			enemies.add(e);
			e.shotEnemy();// 创建敌方坦克时发子弹
		}
		try {
			img1 = ImageIO.read(new File("images/bomb1.gif"));
			img2 = ImageIO.read(new File("images/bomb2.gif"));
			img3 = ImageIO.read(new File("images/bomb3.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public MyPanel(Vector<Enemy> enemies, int enemSize) {
		hero = new Hero(100, 270, 0, 3, Color.YELLOW);// 创建我方坦克
		for (int i = 0; i < enemSize; i++) {// 创建敌方坦克
			Enemy e = new Enemy(enemies.get(i).x, enemies.get(i).y, enemies.get(i).direct, 1, Color.BLUE);
			e.setEnemies(this.enemies);
			new Thread(e).start();
			this.enemies.add(e);
			e.shotEnemy();// 创建敌方坦克时发子弹
		}
		try {
			img1 = ImageIO.read(new File("images/bomb1.gif"));
			img2 = ImageIO.read(new File("images/bomb2.gif"));
			img3 = ImageIO.read(new File("images/bomb3.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 400, 300);// 绘制背景色
		showInfo(g);
		if (hero.isLive) {
			drawTank(g, hero.x, hero.y, hero.direct, hero.color);// 画我方坦克
		}
		for (int i = 0; i < hero.shots.size(); i++) {
			Shot s = hero.shots.get(i);
			if (s.isLive) {// 画我方坦克子弹
				g.setColor(Color.YELLOW);
				g.draw3DRect(s.x, s.y, 1, 1, false);
			} else {
				hero.shots.remove(s);// 删除死亡子弹
			}
		}
		for (int i = 0; i < enemies.size(); i++) {// 画敌方坦克
			Enemy e = enemies.get(i);
			if (e.isLive) {
				drawTank(g, e.x, e.y, e.direct, e.color);
				for (int j = 0; j < e.shots.size(); j++) {
					Shot s = e.shots.get(j);
					if (s.isLive) {// 画敌方坦克子弹
						g.draw3DRect(s.x, s.y, 1, 1, false);
					} else {
						e.shots.remove(s);// 删除死亡子弹
					}
				}
			} else {
				enemies.remove(e);// 删除死亡坦克
			}

		}

		for (int i = 0; i < bombs.size(); i++) {// 画炸弹
			Bomb b = bombs.get(i);
			if (b.life > 6) {
				g.drawImage(img1, b.x, b.y, 30, 30, this);
			} else if (b.life > 3) {
				g.drawImage(img2, b.x, b.y, 30, 30, this);
			} else {
				g.drawImage(img3, b.x, b.y, 30, 30, this);
			}
			b.lifeDown();
			if (b.life == 0) {
				bombs.remove(b);
			}
		}
	}

	public void showInfo(Graphics g) {
		this.drawTank(g, 80, 330, 0, Color.BLUE);
		g.setColor(Color.BLACK);
		g.drawString(Record.enemyNum + "", 110, 350);
		this.drawTank(g, 150, 330, 0, Color.YELLOW);
		g.setColor(Color.BLACK);
		g.drawString(Record.myLife + "", 180, 350);
		g.setFont(new Font("宋体", Font.BOLD, 20));
		g.drawString("你的总成绩：", 420, 20);
		this.drawTank(g, 420, 60, 0, Color.BLUE);
		g.setColor(Color.BLACK);
		g.drawString(Record.dieEnemyNum + "", 460, 80);
	}

	public void drawTank(Graphics g, int x, int y, int direct, Color color) {// 绘制坦克
		g.setColor(color);
		switch (direct) {
		case 0:// 向上
			g.fill3DRect(x, y, 5, 30, false);
			g.fill3DRect(x + 15, y, 5, 30, false);
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			g.fillOval(x + 5, y + 10, 10, 10);
			g.drawLine(x + 10, y + 15, x + 10, y);
			break;
		case 1:// 向下
			g.fillRect(x, y, 5, 30);
			g.fillRect(x + 15, y, 5, 30);
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			g.fillOval(x + 5, y + 10, 10, 10);
			g.drawLine(x + 10, y + 15, x + 10, y + 30);
			break;
		case 2:// 向左
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			g.fillOval(x + 10, y + 5, 10, 10);
			g.drawLine(x + 15, y + 10, x, y + 10);
			break;
		case 3:// 向右
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			g.fillOval(x + 10, y + 5, 10, 10);
			g.drawLine(x + 15, y + 10, x + 30, y + 10);
			break;
		}

	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyChar() == 'w') {// 我方坦克向上走
			hero.direct = 0;
			hero.moveUp();
		} else if (e.getKeyChar() == 's') {// 我方坦克向下走
			hero.direct = 1;
			hero.moveDown();
		} else if (e.getKeyChar() == 'a') {// 我方坦克向左走走
			hero.direct = 2;
			hero.moveLeft();
		} else if (e.getKeyChar() == 'd') {// 我方坦克向右走
			hero.direct = 3;
			hero.moveRight();
		}
		if (e.getKeyChar() == 'j') {
			System.out.println(hero);
			if (hero.shots.size() <= 5 && hero.isLive) {
				hero.shotEnemy();
			}
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	public void hitTank(Shot s, Tank t) {// 子弹和坦克的碰撞检测
		switch (t.direct) {
		case 0:
		case 1:
			if (s.x > t.x && s.x < t.x + 20 && s.y > t.y && s.y < t.y + 30) {
				s.isLive = false;
				t.isLive = false;
				Record.reduceNum(t);
				Bomb b = new Bomb(t.x, t.y);
				bombs.add(b);
			}
			break;

		case 2:
		case 3:
			if (s.x > t.x && s.x < t.x + 30 && s.y > t.y && s.y < t.y + 20) {
				s.isLive = false;
				t.isLive = false;
				Record.reduceNum(t);
				Bomb b = new Bomb(t.x, t.y);
				bombs.add(b);
			}
			break;
		}
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for (Shot s : hero.shots) {// 我方子弹和敌方坦克碰撞检测
				for (Enemy e : enemies) {
					hitTank(s, e);
				}
			}
			for (Enemy e : enemies) {
				if (hero.isLive) {// 敌方子弹和我方坦克碰撞检测
					for (Shot s : e.shots) {
						hitTank(s, hero);
					}
				}

			}
			repaint();
		}
	}

}
